from appserver import db_store


"""
传感器与设备的中间表
"""

class DistrictWithDevice(db_store.Model):
    __tablename__ = "district_with_device"
    __bind_key__ = "main"

    # id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键')
    device_id = db_store.Column(db_store.Integer,db_store.ForeignKey("devices.id"),primary_key=True,nullable=False,comment="设备id")
    district_id = db_store.Column(db_store.Integer,db_store.ForeignKey("district.id"),primary_key=True,nullable=False,comment="分区id")

    def __repr__(self):
        return "<DistrictWithDevice %s>" % self.id

    def save(self):
        try:
            db_store.session.add(self)
            db_store.session.commit()
            return True
        except Exception as e:
            db_store.session.rollback()
            print(e)
            return False
    
    def delete(self):
        try:
            db_store.session.delete(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print(e)
            db_store.session.rollback()
            return False